############################
##Matrix Completion Method##
############################

install.packages("fastplm")
install.packages("panelView")
install.packages("fect")
library(readr)
set.seed(1234)
library(fect)
data(fect)
ls()
library(panelView)
library(patchwork)
library(readr)

 
##BASELINE##
#MC# (Model 1.out.mc.p)
rebldlyb <- read.csv("Brancati_MC_no50miss62.csv")


out.mc.p1 <- fect(rebelattk_isodly ~ lockdown_oxf50 + stateattk_isowkly_lag +deaths_wklag+exit +entrance, data = rebldlyb, index = c("isonum","seqdays"),
force = "two-way", method = "mc", lambda = out.mc$lambda.cv,
CV = 0, parallel = TRUE, se = TRUE,
nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0))

print(out.mc.p1)

plot(out.mc.p1, main = "Estimated ATT (MC)")
plot(out.mc.p1, vis = "none", cex.text = 0.8, stats = c("placebo.p","equiv.p"), main = "Estimated ATT (MC)")


###POP###
#Pre-trends: Equivalence test:  TOST=Reject the null (0.000) to pass.
# A larger placebo p-value and a smaller placebo TOST (Equivalence Test) p-value are preferred.

rebldlypop <- read.csv("Brancati_MC_62Adm.csv")
out.mc.p2 <- fect(rebelattk_admdly~ lockdown_oxf50 + stateattk_admwkly_lag +deaths_wklag+exit +entrance + state_territory + contested_territory, data = rebldlypop, index = c("isodistrictid","seqdays"),
force = "two-way", method = "mc", lambda = out.mc$lambda.cv,
CV = 0, parallel = TRUE, se = TRUE,
nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0))

print(out.mc.p2)
plot(out.mc.p2, vis = "none", cex.text = 0.8, stats = c("placebo.p","equiv.p"), main = "Estimated ATT (MC)")

##CARRY-OVER TESTS###
# If carryover effects do not exist, we would expect the average prediction error in few periods immediately post-treatment to be close to zero.
 
out.mc.c <- fect(rebelattk_admdly~ lockdown_oxf50 + stateattk_admwkly_lag +deaths_wklag+exit +entrance + state_territory + contested_territory, data = rebldlypop, index = c("isodistrictid","seqdays"),
force = "two-way", method = "mc", lambda = out.mc$lambda.cv,
CV = 0, parallel = TRUE, se = TRUE,
nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 3))

plot(out.mc.c, type = "exit", ylim = c(-2.5,4.5), cex.text = 0.8, main = "Estimated ATT (MC)")




